package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyRateDAO;
import com.mbc.common.dao.CurrencyRateDetailDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.CurrencyRateDetail;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.DataNotFoundException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaRMParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.AgentBalanceDAO;
import com.mbc.rmt.dao.AgentCOADAO;
import com.mbc.rmt.dao.AgentCashTransactionDAO;
import com.mbc.rmt.dao.AgentDAO;
import com.mbc.rmt.dao.BankDAO;
import com.mbc.rmt.dao.General_COADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.AgentCashTransactionDataList;
import com.mbc.rmt.entities.Agent;
import com.mbc.rmt.entities.AgentBalance;
import com.mbc.rmt.entities.AgentCOA;
import com.mbc.rmt.entities.AgentCashTransaction;
import com.mbc.rmt.entities.AgentCashTransactionRpt;
import com.mbc.rmt.entities.Bank;
import com.mbc.rmt.entities.GeneralLedger;
import com.mbc.rmt.entities.General_COA;
import com.mbc.rmt.manager.DefinePaymentTypeMgr.GL_STATUS;

public class AgentCashTransactionMgr extends AbstractMgr{
	
	private AgentCashTransactionDAO agentcashtransactionDAO;
	public static enum STATUS { ADD, UPDATE, DELETE }
	private AgentBalanceDAO agentbalanceDAO;
	private CurrencyRateDetailDAO currencyrateDetailDAO;
	private AgentCOADAO agentCOADAO;
	private AgentDAO agentDAO;
	private CurrencyRateDAO currencyRateDAO;
	private General_COADAO generalCOADAO;
	private BankDAO bankDAO;
	private LogDAO logDAO;
	public AgentCashTransactionMgr() {
	}
	
	public AgentCashTransactionMgr(String sessionId) {
		this.sessionId = sessionId;
	}
	public String create(AgentCashTransaction entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			agentcashtransactionDAO = RemittanceDAOFactory.getAgentCashTransactionDAO(dbManager.getConnection());
			currencyrateDetailDAO = CommonDAOFactory.getCurrencyRateDetailDAO(dbManager.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			String voucher = "";
			if(entity.getInvoice_Id() == 0){
				AgentCashTransaction l_info = new AgentCashTransaction();
				l_info = agentcashtransactionDAO.find(entity.getInvoice_Id());			
				if (l_info != null) {
					throw new Exception("Invoice ID already exists!");
				} else {					
					GoToAgentBalance(STATUS.ADD, entity);
					
					CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();					
					l_currencyrateDetail = currencyrateDetailDAO.findLatest(entity.getRateid());
					
					if (l_currencyrateDetail.getRate()  !=  entity.getRate()){					  
				   
					   l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
					   l_currencyrateDetail.setCurrencyRateId(entity.getRateid());
					   l_currencyrateDetail.setRate(entity.getRate());
					   l_currencyrateDetail.seteDateTime(entity.getInvoiceDate());
					   currencyrateDetailDAO.create(l_currencyrateDetail);
					   
					   l_logentity.setTablename("CurrencyRateDetail");
					   l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					   l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
					   l_logentity.setLogstatus(Log.__SAVE_DATA);
					   logDAO.getData(l_logentity);
					}
					entity.setInvoice_Id(KeyGenerator.generateKey(getConnection()));
					if(entity.getTransactionType() == 0)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_RECEIVED_FROM_AGENT, entity.getInvoiceDate(), entity.getDefaultBranchId(), getConnection());
					else
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_PAID_TO_AGENT, entity.getInvoiceDate(), entity.getDefaultBranchId(), getConnection());
					//entity.setInvoiceNo(CashReceivedSerialMgr.generateCashReceivedNo(getConnection(), entity.getDefaultBranchId(), entity.getInvoiceDate()));
					
					entity.setInvoiceNo(voucher);
					
					gotoGL(entity, GL_STATUS.CREATE);
					agentcashtransactionDAO.create(entity);
					
					l_logentity.setTablename("AgentCashTransaction");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(entity.getInvoice_Id());
					l_logentity.setUserid(entity.getUserId());
					l_logentity.setLogstatus(Log.__SAVE_DATA);
					logDAO.getData(l_logentity);
					}
			}
			else{
				GoToAgentBalance(STATUS.UPDATE, entity);
				
				CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();					
				l_currencyrateDetail = currencyrateDetailDAO.findLatest(entity.getRateid());
				
				if (l_currencyrateDetail.getRate()  !=  entity.getRate()){					  
			   
				   l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
				   l_currencyrateDetail.setCurrencyRateId(entity.getRateid());
				   l_currencyrateDetail.setRate(entity.getRate());
				   l_currencyrateDetail.seteDateTime(entity.getInvoiceDate());
				   currencyrateDetailDAO.create(l_currencyrateDetail);
				   
				   l_logentity.setTablename("CurrencyRateDetail");
				   l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				   l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
				   l_logentity.setLogstatus(Log.__SAVE_DATA);
				   logDAO.getData(l_logentity);
				}
				
				agentcashtransactionDAO = RemittanceDAOFactory.getAgentCashTransactionDAO(dbManager.getConnection());
				agentcashtransactionDAO.update(entity);
				
				l_logentity.setTablename("AgentCashTransaction");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getInvoice_Id());
				l_logentity.setUserid(entity.getUserId());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
				
				gotoGL(entity, GL_STATUS.CREATE);
			}
			commintTransaction();
			return voucher;
		} catch (SQLException e) {
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	

	public void update(AgentCashTransaction entity) throws Exception {
		openConnection();
		try {
	
		} finally {
			closeConnection();
		}
	}

	public void delete(AgentCashTransaction entity,long userId, Date deletedDate) throws Exception {
		openConnection();
		try {
			agentcashtransactionDAO = RemittanceDAOFactory.getAgentCashTransactionDAO(dbManager
					.getConnection());
			agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager
					.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity = new Log();
			l_logentity.setTablename("AgentCashTransaction");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getInvoice_Id());
			l_logentity.setUserid(userId);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			agentcashtransactionDAO.delete(entity);
//			AgentBalance l_agent = new AgentBalance();
//			l_agent.setCurrencyid(entity.getCurrencyId());
//			l_agent.setAgent_id(entity.getAgentId());
//			l_agent = agentbalanceDAO.getInfo(l_agent);
//			if(l_agent != null)
//			{
//				l_agent.setAgent_id(entity.getAgentId());
//				l_agent.setCurrencyid(entity.getCurrencyId());
//				l_agent.setCurrentbalance(l_agent.getCurrentbalance()-entity.getPaidAmount());
//				agentbalanceDAO.update(l_agent);
//			}
			GoToAgentBalance(STATUS.DELETE, entity);
			gotoGL(entity, GL_STATUS.DELETE);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AgentCashTransactionDataList findAgentCashTranDataList(CriteriaRMParams cri)
			throws Exception {
		openConnection();
		try {

			
			
			agentcashtransactionDAO = RemittanceDAOFactory.getAgentCashTransactionDAO(dbManager
					.getConnection());
			return agentcashtransactionDAO.findAgentCashTranDataList(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}	

	public AgentCashTransaction getAgentCashTransaction(AgentCashTransaction agentcashtransaction) throws Exception {
		openConnection();
		try {
			agentcashtransactionDAO = RemittanceDAOFactory.getAgentCashTransactionDAO(dbManager
					.getConnection());
			return agentcashtransactionDAO.getAgentCashTransaction(agentcashtransaction);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}	
	public void GoToAgentBalance(STATUS Status,AgentCashTransaction entity) throws Exception {
		try {
			agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager
					.getConnection());
			AgentBalance l_agent = new AgentBalance();
			if(Status == STATUS.ADD){
				l_agent = new AgentBalance();
				l_agent.setAgent_id(entity.getAgentId());
				l_agent.setCurrencyid(entity.getCurrencyId());
				l_agent = agentbalanceDAO.getInfo(l_agent);
				if(l_agent == null)
				{
					l_agent = new AgentBalance();
					l_agent.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
					l_agent.setAgent_id(entity.getAgentId());
					l_agent.setCurrencyid(entity.getCurrencyId());
					if(entity.getTransactionType() == 1)
						l_agent.setCurrentbalance(entity.getPaidAmount());
					else
						l_agent.setCurrentbalance(-entity.getPaidAmount());
					agentbalanceDAO.create(l_agent);
				}
				else
				{
					if(entity.getTransactionType() == 1)
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()+entity.getPaidAmount());
					else
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()-entity.getPaidAmount());
					agentbalanceDAO.update(l_agent);
				}
			}
			else if(Status == STATUS.UPDATE)
			{
				l_agent = new AgentBalance();
				double amt = 0;
				double tempamt = 0;
				amt = entity.getPaidAmount();
				tempamt = entity.getTempamount();
				long currencyid= entity.getCurrencyId();
				long tempcur = entity.getTempcurrencyid();
				long agent = entity.getAgentId();
				long tempagent = entity.getTempagentid();
				if(tempcur == currencyid && tempagent == agent)
						{
							l_agent.setAgent_id(entity.getAgentId());
							l_agent.setCurrencyid(entity.getCurrencyId());	
							l_agent = agentbalanceDAO.getInfo(l_agent);
							double balanceamt = 0;

							if(tempamt > amt){
								balanceamt = tempamt - amt;
								if(entity.getTransactionType() == 1)
									l_agent.setCurrentbalance(l_agent.getCurrentbalance() + balanceamt);
								else
									l_agent.setCurrentbalance(l_agent.getCurrentbalance() - balanceamt);
									
							}
							else if(tempamt < amt){
								balanceamt = amt - tempamt;
								if(entity.getTransactionType() == 1)
									l_agent.setCurrentbalance(l_agent.getCurrentbalance() + balanceamt);
								else
									l_agent.setCurrentbalance(l_agent.getCurrentbalance() - balanceamt);
							}
							else								
								l_agent.setCurrentbalance(l_agent.getCurrentbalance());
						}
						else
						{
							double amount = 0;
							l_agent.setCurrencyid(entity.getTempcurrencyid());
							l_agent.setAgent_id(entity.getTempagentid());
							l_agent = agentbalanceDAO.getInfo(l_agent);
							if(entity.getTransactionType() == 1)
								amount = l_agent.getCurrentbalance() + tempamt;
							else
								amount = l_agent.getCurrentbalance() - tempamt;
							
							l_agent.setCurrentbalance(amount);
							agentbalanceDAO.update(l_agent);			
							
							l_agent.setAgent_id(entity.getAgentId());
							l_agent.setCurrencyid(entity.getCurrencyId());	
							l_agent = agentbalanceDAO.getInfo(l_agent);
							if(l_agent == null)
							{
								l_agent = new AgentBalance();
								l_agent.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
								l_agent.setAgent_id(entity.getAgentId());
								l_agent.setCurrencyid(entity.getCurrencyId());
								l_agent.setOpeningbalance(0);
								if(entity.getTransactionType() == 1)
									l_agent.setCurrentbalance(amt);
								else
									l_agent.setCurrentbalance(-amt);
					
								agentbalanceDAO.create(l_agent);
							}
							else
							{
								if(entity.getCurrencyId() == entity.getTempcurrencyid() && entity.getAgentId() == entity.getTempagentid()){
									if(entity.getTransactionType() == 1)
										l_agent.setCurrentbalance(amount+amt);
									else
										l_agent.setCurrentbalance(amount-amt);
								}
								else{
									if(entity.getTransactionType() == 1)
										l_agent.setCurrentbalance(l_agent.getCurrentbalance() + amt);
									else
										l_agent.setCurrentbalance(l_agent.getCurrentbalance() - amt);
								}
							}
						}
						agentbalanceDAO.update(l_agent);
			}
			else
			{
				l_agent = new AgentBalance();
				l_agent.setCurrencyid(entity.getCurrencyId());
				l_agent.setAgent_id(entity.getAgentId());
				l_agent = agentbalanceDAO.getInfo(l_agent);
				if(l_agent != null)
				{
					l_agent.setAgent_id(entity.getAgentId());
					l_agent.setCurrencyid(entity.getCurrencyId());
					if(entity.getTransactionType() == 1)
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()+entity.getPaidAmount());
					else
						l_agent.setCurrentbalance(l_agent.getCurrentbalance()-entity.getPaidAmount());
					
					agentbalanceDAO.update(l_agent);
				}
			}
		} catch(Exception e) { throw e; }
		/* -- END GL ADD DATA -- */
	} 
	public void gotoGL(AgentCashTransaction l_entity, GL_STATUS status) throws Exception {
		
		currencyRateDAO = CommonDAOFactory.getCurrencyRateDAO(getConnection());
		agentDAO = RemittanceDAOFactory.getAgentDAO(getConnection());
		agentCOADAO = RemittanceDAOFactory.getAgentCOADAO(getConnection());	
		
		generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());		
		bankDAO = RemittanceDAOFactory.getBankDAO(getConnection());
		
		GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
		if(status == GL_STATUS.DELETE){
			if(l_entity.getTransactionType() == 0)
				l_glmgr.deleteGL(l_entity.getInvoiceNo() + "-CR");
			else
				l_glmgr.deleteGL(l_entity.getInvoiceNo() + "-CP");
		}
		if(status == GL_STATUS.CREATE) {
			Agent l_agent = agentDAO.findById(l_entity.getAgentId());
			AgentCOA l_agentCOA = agentCOADAO.findByCode(l_agent.getAccountset_code(), l_entity.getDefaultBranchId());
			
			//AgentCOA l_agentCOA = agentCOADAO.find(l_agent.getAccountset_id());
			General_COA l_generalCOA = generalCOADAO.find(l_entity.getDefaultBranchId());
			Bank l_Bank = bankDAO.findByCode(l_entity.getBankcode(), l_entity.getDefaultBranchId());
			
			if(l_agentCOA == null)
				throw new DataNotFoundException("Plz fill AgentCOA");
			
			GeneralLedger _glHeader = new GeneralLedger();
			//_glHeader.setGlKey(KeyGenerator.generateKey(getConnection()));
			_glHeader.setGlKey(l_glmgr.getGLKey());
			if(l_entity.getTransactionType() == 0)
				_glHeader.setSlip_ID(l_entity.getInvoiceNo() + "-CR");
			else
				_glHeader.setSlip_ID(l_entity.getInvoiceNo() + "-CP");
			_glHeader.setSlipDate(l_entity.getInvoiceDate());
			_glHeader.setSystemDate(new Date());
			_glHeader.setStatus("JH");
			_glHeader.setSrNo(1);
			_glHeader.setOffice_ID(l_entity.getDefaultBranchId());			

			CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getCurrencyId(), l_entity.getDefaultBranchId());
			
			if(l_entity.getTransactionType() == 0){
				GeneralLedger _glDebit = new GeneralLedger();
				//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));			
				_glDebit.setGlKey(l_glmgr.getGLKey());
				_glDebit.setSlip_ID(l_entity.getInvoiceNo() + "-CR");
				_glDebit.setSlipDate(l_entity.getInvoiceDate());
				_glDebit.setSystemDate(new Date());
				if(l_entity.getBankId() == null){
					_glDebit.setCoaKey(l_generalCOA.getCashAccountID());
					_glDebit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
				}else{
					_glDebit.setCoaKey(l_Bank.getCoaid());
					_glDebit.setDepartmentKey(l_Bank.getDepartmentid());
				}
				_glDebit.setCurrencyKey(l_entity.getCurrencyId());
				_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glDebit.setExchangeRate(l_currencyrate.getRate());
				_glDebit.setSourceAmount(l_entity.getPaidAmount());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glDebit.setHomeAmount((l_entity.getPaidAmount()) * l_currencyrate.getRate());
				else
					_glDebit.setHomeAmount((l_entity.getPaidAmount() / l_currencyrate.getRate()));
				
				_glDebit.setStatus("JCR");	
				_glDebit.setSrNo(1);
				_glDebit.setOffice_ID(l_entity.getDefaultBranchId());			
				l_glmgr.createGL(_glDebit);		
				
				GeneralLedger _glCredit = new GeneralLedger();
				//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
				_glCredit.setGlKey(l_glmgr.getGLKey());
				_glCredit.setSlip_ID(l_entity.getInvoiceNo() + "-CR");
				_glCredit.setSlipDate(l_entity.getInvoiceDate());
				_glCredit.setSystemDate(new Date());
				_glCredit.setCoaKey(l_agentCOA.getAccpayable());
				_glCredit.setDepartmentKey(l_agentCOA.getAccpayable_dept());			
				_glCredit.setCurrencyKey(l_entity.getCurrencyId());
				_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glCredit.setExchangeRate(l_currencyrate.getRate());
				_glCredit.setSourceAmount(-(l_entity.getPaidAmount()));
				
				if(l_currencyrate.getRoper().equals("M"))
					_glCredit.setHomeAmount(-(l_entity.getPaidAmount() * l_currencyrate.getRate()));
				else
					_glCredit.setHomeAmount(-(l_entity.getPaidAmount() / l_currencyrate.getRate()));
				
				_glCredit.setStatus("JCR");
				_glCredit.setSrNo(2);
				_glCredit.setOffice_ID(l_entity.getDefaultBranchId());
				
				l_glmgr.createGL(_glCredit);
			}else{
				GeneralLedger _glDebit = new GeneralLedger();
				//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));			
				_glDebit.setGlKey(l_glmgr.getGLKey());
				_glDebit.setSlip_ID(l_entity.getInvoiceNo() + "-CP");
				_glDebit.setSlipDate(l_entity.getInvoiceDate());
				_glDebit.setSystemDate(new Date());
				_glDebit.setCoaKey(l_agentCOA.getAccpayable());
				_glDebit.setDepartmentKey(l_agentCOA.getAccpayable_dept());	
				_glDebit.setCurrencyKey(l_entity.getCurrencyId());
				_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glDebit.setExchangeRate(l_currencyrate.getRate());
				_glDebit.setSourceAmount(l_entity.getPaidAmount());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glDebit.setHomeAmount((l_entity.getPaidAmount()) * l_currencyrate.getRate());
				else
					_glDebit.setHomeAmount((l_entity.getPaidAmount() / l_currencyrate.getRate()));
				
				_glDebit.setStatus("JCP");	
				_glDebit.setSrNo(1);
				_glDebit.setOffice_ID(l_entity.getDefaultBranchId());			
				l_glmgr.createGL(_glDebit);		
				
				GeneralLedger _glCredit = new GeneralLedger();
				//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
				_glCredit.setGlKey(l_glmgr.getGLKey());
				_glCredit.setSlip_ID(l_entity.getInvoiceNo() + "-CP");
				_glCredit.setSlipDate(l_entity.getInvoiceDate());
				_glCredit.setSystemDate(new Date());
				if(l_entity.getBankId() == null){
					_glCredit.setCoaKey(l_generalCOA.getCashAccountID());
					_glCredit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
				}else{
					_glCredit.setCoaKey(l_Bank.getCoaid());
					_glCredit.setDepartmentKey(l_Bank.getDepartmentid());
				}				
				_glCredit.setCurrencyKey(l_entity.getCurrencyId());
				_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glCredit.setExchangeRate(l_currencyrate.getRate());
				_glCredit.setSourceAmount(-(l_entity.getPaidAmount()));
				
				if(l_currencyrate.getRoper().equals("M"))
					_glCredit.setHomeAmount(-(l_entity.getPaidAmount() * l_currencyrate.getRate()));
				else
					_glCredit.setHomeAmount(-(l_entity.getPaidAmount() / l_currencyrate.getRate()));
				
				_glCredit.setStatus("JCP");
				_glCredit.setSrNo(2);
				_glCredit.setOffice_ID(l_entity.getDefaultBranchId());
				
				l_glmgr.createGL(_glCredit);
			}
			l_glmgr.createGL(_glHeader);
		}
	}

	
	//Report
		public JasperPrint prepareAgentCashTransactionReport(AgentCashTransactionRpt info)
				throws Exception {
			openConnection();
			JasperPrint l_jsPrint = null;
			try {
				agentcashtransactionDAO = RemittanceDAOFactory.getAgentCashTransactionDAO(dbManager.getConnection());
				JRResultSetDataSource l_jrRS = new JRResultSetDataSource(agentcashtransactionDAO.prepareAgentCashTransactionReport(info));
				JasperReport l_jsRpt = null;
				if(info.getOrderby().equals("currencyid"))
					l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/AgentCashTransactionByCurRpt.jasper");
				else
					l_jsRpt = (JasperReport) JRLoader
						.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/AgentCashTransactionRpt.jasper");
		
				HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
				l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

			} catch (JRException je) {
				throw je;
			} catch (Exception e) {
				throw e;
			} finally {
				closeConnection();
			}

			return l_jsPrint;

		}
		
		public AgentCashTransaction getAgentbalance(AgentCashTransaction agent) throws Exception {
			openConnection();
			try{
				agentcashtransactionDAO = RemittanceDAOFactory.getAgentCashTransactionDAO(dbManager
						.getConnection());
					return agentcashtransactionDAO.getAgentbalance(agent);
			} catch (Exception e) {
				throw e;
			} finally {
				closeConnection();
			}
		}
		
		
}
